<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2017/1/10
 * Time: 10:51
 */
use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
?>
<script>
    //////////////////////////////////////////////////////////////
    //混合支付入口  需要购买的积分,需要的现金 单位：元，订单编号  身份证姓名  身份证号码  订单备注
    invokeCashPay=function(integral,pay_cash_f,orderCode,idcard_name,idcard_number,remark){
        //1
        $('.pay-window1 .money span').html(pay_cash_f+'元');
        mallPayCash_orderCode=orderCode;
        mallPayCash_integral=integral;
        mallPayCash_idcard_name=idcard_name;
        mallPayCash_idcard_number=idcard_number;
        mallPayCash_remark=remark;
        $('.pay-window1').show();//2

    };
    var mallPayCash_orderCode=false;
    var mallPayCash_integral=false;
    var mallPayCash_idcard_name=false;
    var mallPayCash_idcard_number=false;
    var mallPayCash_remark=false;
    ////////////////////////////////////////////////////////////////
    //生成订单
    create_order = function (idcard_name,idcard_number,remark) {
        var data = {};
        data.remark = remark;
        data.idcard_name = idcard_name;
        data.idcard_number = idcard_number;

        //生成订单
        var re = requestJsonMallM('create-order-handle', data);
        if (re.status == 0) {
            var createOrderResult = re.data.createOrderResult;
            if (createOrderResult.status == '0') {
                var orderCode = createOrderResult.orderCode;
                return orderCode;
            } else {
                alert(createOrderResult.message);
            }
        } else {
            alert(re.message);
        }
        return false;
    };
    function showWxPayCode(integral,orderCode){
        var data={};
        data.integral=integral;
        data.orderCode=orderCode;
        data.trade_type='NATIVE';

        var re=requestJsonMallM("<?=Url::to(['pay-integral/pc-weixin-pay-handle'])?>",data);
        if(re.status==0) {
            var code_url=re.data.code_url;
            out_trade_no=re.data.out_trade_no;
            $('#weixin_pay_code').attr('src','<?=Url::to(['mall-m/qr-code-out'])?>?text='+encodeURI(code_url));
            return true;
        }else {
            alert('error:'+re.message);
            return false;
        }
    }

    // 循环等待 查询现金支付记录  入口
    function start_waitting_pay(loop_num){
        waitting_loop_num=loop_num;
        isWattingGetPayCash=true;
        getPayCash();
    }
    var out_trade_no=false;
    var isWattingGetPayCash=false;
    var waitting_num=0;
    var waitting_loop_num=20;
    getPayCash=function(){
        if(isWattingGetPayCash==false){
            return;
        }
        if(waitting_num>waitting_loop_num){
            alert('支付等待时间超时!');
            href_order_detail_mallPayCash();
            return;
        }
        waitting_num++;
        isWattingGetPayCash=false;
        var data={};
        data.out_trade_no=out_trade_no;
        var reMix=requestJsonMallM('<?=Url::to(['mall-m/get-pay-integral-record-handle'])?>',data,true,function(re){
            if(re.status!=0){

                alert(re.message);
            }else{
                if(re.data.cash_status==1){
                    //支付完成
                    isWattingGetPayCash=false;
                    href_order_success_mallPayCash();
                }else {
                    isWattingGetPayCash = true;
                    setTimeout('getPayCash()',1000);
                }
            }
        });
    };
    href_order_detail_mallPayCash=function(){
        window.location.href="order-detail?orderCode="+mallPayCash_orderCode;
    };
    href_order_success_mallPayCash=function(){
        window.location.href = "<?=Url::to(['mall/pay-order-success', 'institution_code' => Mall::getCurrentInstitutionCode()])?>" + '&orderCode=' + mallPayCash_orderCode;
    };
</script>

<script>
    $(function(){
        //选择支付方式
        $("#btn_pay_window1_ok").click(function(){
            $(".pay-window1").hide();
            //3
            if(mallPayCash_orderCode==false){
                waittingShow('生成订单中..');
                mallPayCash_orderCode=create_order(mallPayCash_idcard_name,mallPayCash_idcard_number,mallPayCash_remark);
            }
            if(mallPayCash_orderCode==false){
                waittingHide();
                return;
            }
            var payType=$('.pay-way .dot-chosen').attr('payType');
            //微信
            // 4
            if(payType=="weixin"){
                $(".pay-window2").show();
                waittingHide();
                var result=showWxPayCode(mallPayCash_integral,mallPayCash_orderCode);
                if(result==false){
                    $(".pay-window2").hide();
                }else{
                    //5
                    //等待微信扫码支付..
                    start_waitting_pay(300);//等5 * 60秒
                }
            }
            if(payType=="ali"){
                $('#alipayment [name="integral"]').val(mallPayCash_integral);
                $('#alipayment [name="order_code"]').val(mallPayCash_orderCode);
                $('#alipayment').submit();
            }
            //支付宝
            //pass
        });
        //选择支付方式  取消
        $("#btn_pay_window1_cancel").click(function(){
            $(".pay-window1").hide();
            waittingHide();
        });
        //微信扫码取消
        $("#btn_pay_weixin_cancel").click(function(){
            $(".pay-window2").hide();
            waittingHide();
            href_order_detail_mallPayCash();
        });
    });
</script>
<script type="text/javascript">
    $(function(){

        $(".way-1ist").click(function(){
            $(this).find(".dot").addClass("dot-chosen");
            $(this).siblings().find(".dot").removeClass("dot-chosen");
        });
    });
</script>
<style type="text/css">
    .pay-window1 .pay-shade{
        width: 100%;
        height: 100%;
        position: fixed;
        left: 0;
        top: 0;
        z-index: 999;
        background: rgba(0,0,0,.7);
    }
    .pay-window1 .content{
        width: 600px;
        height: 400px;
        background: #FFFFFF;
        position: fixed;
        left: 50%;
        top: 50%;
        margin-left: -300px;
        margin-top: -200px;
        z-index: 9999;
    }
    .pay-window1 .content .money{
        width: 500px;
        height: 40px;
        line-height: 40px;
        padding-left: 100px;
        background: #f3f3f3;
        margin-top: 36px;
        font-size: 14px;
        color: #333333;
        font-family: "微软雅黑";
    }
    .pay-window1 .content .money span{
        color: #E1515B;
    }
    .pay-window1 .content .pay-way{
        margin-top: 44px;
        margin-left: 90px;
        overflow: hidden;
    }
    .pay-window1 .content .pay-way ul li{
        float: left;
        margin-right: 60px;
        height: 44px;
        cursor: pointer;
    }
    .pay-window1 .content .pay-way ul li .dot{
        float: left;
        width: 19px;
        height: 19px;
        margin-top: 12px;
        margin-right: 8px;
        border-radius:50% ;
        border: 1px solid #dadada;
    }
    .dot-chosen{
        width: 20px !important;
        height: 20px !important;
        background: url(<?=$web?>/images/mall/pay-window-dot.png) no-repeat;
        border: none !important;
    }
    .pay-window1 .content .pay-way ul li img{
        width: 144px;
        height: 44px;
    }
    .pay-window1 .content .text{
        margin-top: 70px;
        margin-left: 90px;
    }
    .pay-window1 .content .text .title{
        font-size: 16px;
        font-family: "微软雅黑";
        color: #727171;
        line-height: 25px;
    }
    .pay-window1 .content .text .txt{
        font-size: 14px;
        font-family: "微软雅黑";
        color: #9fa0a0;
    }
    .pay-window1 .content .btn{
        margin-top: 40px;
        margin-left: 120px;
    }
    .pay-window1 .content .btn .cancel-btn{
        float: left;
        margin-right:90px ;
        cursor: pointer;
        display: block;
        width: 140px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        font-family: "微软雅黑";
        font-size: 14px;
        color: #333333;
        background: #e5e5e5;
    }
    .pay-window1 .content .btn .cancel-btn:hover{
        background: #F3F3F3;
    }
    .pay-window1 .content .btn .confirm-btn{
        float: left;
        cursor: pointer;
        display: block;
        width: 140px;
        height: 40px;
        text-align: center;
        line-height: 40px;
        font-family: "微软雅黑";
        font-size: 14px;
        color: #FFFFFF;
        background: #E1515B;
    }
    .pay-window1 .content .btn .confirm-btn:hover{
        background: #E07E84;
    }
</style>
<div class="pay-window1" style="display: none;">
    <div class="content">
        <p class="money">支付金额：<span>100元</span></p>
        <div class="pay-way">
            <ul>
                <li class="way-1ist">
                    <div class="dot" payType="ali"></div>
                    <img src="<?=$web?>/images/mall/window-alipay-pay.jpg"/>
                </li>
                <li class="way-1ist">
                    <div class="dot dot-chosen" payType="weixin"></div>
                    <img src="<?=$web?>/images/mall/window-wechat-pay.jpg"/>
                </li>
            </ul>
        </div>
        <div class="text">
            <p class="title">您需要支付现金</p>
            <p class="txt">当您取消订单或退货时，支付的现金部分将以积分形式退还到您的帐户。</p>
        </div>
        <div class="btn">
            <a  class="cancel-btn" id="btn_pay_window1_cancel">取消</a>
            <a href="#" id="btn_pay_window1_ok" class="confirm-btn">确定</a>
        </div>
    </div>
    <div class="pay-shade"></div>
</div>

<style type="text/css">
    .pay-window2 .pay-shade-2{
        width: 100%;
        height: 100%;
        position: fixed;
        left: 0;
        top: 0;
        z-index: 999;
        background: rgba(0,0,0,.7);
    }
    .pay-window2 .content-2{
        width: 400px;
        height: 450px;
        background: #FFFFFF;
        position: fixed;
        left: 50%;
        top: 50%;
        margin-left: -200px;
        margin-top: -225px;
        z-index: 9999;
    }
    .pay-window2 .content-2 .name{
        color: #E1515B;
        font-size: 18px;
        font-family: "微软雅黑";
        text-align: center;
        margin-top: 30px;
    }
    .pay-window2 .content-2 img{
        width: 200px;
        height: 200px;
        margin-top: 50px;
        margin-left: 100px;
    }
    .pay-window2 .content-2 .cancel-btn-2{
        display: block;
        text-align: center;
        cursor: pointer;
        margin: 0 auto;
        margin-top: 40px;
        width: 180px;
        height: 44px;
        line-height: 44px;
        color: #FFFFFF;
        background: #999999;
    }
    .pay-window2 .content-2 .cancel-btn-2:hover{
        background: #CCCCCC;
    }


</style>
<div class="pay-window2" style="display: none;">
    <div class="content-2">
        <p class="name">微信扫码支付</p>
        <img id="weixin_pay_code" src=""/>
        <a class="cancel-btn-2" id="btn_pay_weixin_cancel">取消</a>
    </div>
    <div class="pay-shade-2"></div>
</div>
<form id="alipayment" action='<?= Url::to(["pay-integral/mall-alipay-pay"]) ?>' method='post' style="display: none;">
    支付宝支付
    积分:<input name="integral" type="input"/>
    订单号:<input name="order_code" type="input"/>
    pc:<input name="is_pc" value="1" type="input"/>
    <input type="submit" value="支付宝支付" id="btn_ali">
</form>